(function(){
    var urls = {
        sendMailer:"/mailer/sendPage",
        filePrewiew:"/filePrewiew/page",
        fileUpload:"/fileUpload/page"
    };
	$(".rightChange").on("click",function(e){
        e.preventDefault();
        var urlName = $(this).attr("id");
		$("iframe").attr("src",urls[urlName]+"?rando="+Math.random());
        if(urlName !=="fileUpload" && $("iframe").css("display")==="none"){
            loginSuccess(true,"");
        }
	});

    var logined = $("input[name=logined]").val();
    function checkLogin(tab){
        var name = $("#"+tab+" input[name=name]").val();
        var password = $("#"+tab+" input[name=password]").val();
        if(!name){
            return "用户名不能为空！";
        }
        if(!password){
            return "密码不能为空!";
        }
        return {name:name,password:password};
    };
    function checkSign(tab){
        var data = checkLogin(tab);
        if(!data.name){
            return data;
        }
        var repeatpwd = $("#"+tab+" input[name=password2]").val();
        if(!repeatpwd){
            return "请在重复密码框中再次输入密码!";
        }
        if(repeatpwd !== data.password){
            return "两次输入的密码不一致!";
        }
        return data;
    };
    function connect(url, data, callback){
        console.log("to post data",url,data);
        $.post(url,data, function(back){
           callback(back);
        });
    };

    function listenLongin(){ // 监听登录和注册事件
        $("input.submitBtn").on("click", function(){
            var opt = $("#current a").attr("title");
            var data ,url;
            if(opt === "tab1"){
                 data = checkLogin("tab1");
                if(data.name){
                   url = "/user/login";
                }
            }else{
                 data = checkSign("tab2");
                if(data.name){
                    url = "/user/sign";
                }
            }
            console.log("data", data);
            if(!url){
                $("#myModal .modal-body").text(data);
                $("#myModal").modal("show");
                return false;
            }
            connect(url, data, function(back){
                if(back !== "success"){
                    $("#myModal .modal-body").text(back);
                    $("#myModal").modal("show");
                }else{
                    loginSuccess(true, opt);
                }
            });
        });
    };
    if(logined !== "true"){
        listenLongin();
    }
})();

// 页面过期时loginSuccess()--flag = false;tab=null登录/注册成功flag=true tab !=null
function loginSuccess(flag, tab){
    var classChange = flag ? "addClass" : "removeClass";
    var changeCss = flag ? "block" : "none";
    var tishi  ;
    if(!tab&& !flag){
        tishi = "页面过期，请重新登录";
    }else if(tab ==="tab1"){
        tishi = "登录成功"
        $("input[name=logined]").val("true")
    }else if(tab ==="tab2"){
        $("input[name=logined]").val("true")
        tishi = "注册成功"
    }
    $("div.tab")[classChange]("hidden");
    $("input.submitBtn")[classChange]("hidden");
    $("iframe").css("display",changeCss);
    if(tab ===""){
        return false;
    }
    $("input[name=name]").val("");
    $("input[name=password]").val("");
    $("input[name=password2]").val("");
    var alert = '<div class="alert alert-success" style="position: fixed;width: 50%;bottom: 10px;right: 10%;">' +
        ' <a href="#" class="close" data-dismiss="alert">&times;</a>'+tishi+'！</div>';
    $("body").append(alert);
    setTimeout(function(){
        $("div.alert ").remove();
    },2000);
};
$(document).ready(function () {
    $("#content > div").hide(); // Initially hide all content
    $("#tabs li:first").attr("id", "current"); // Activate first tab
    $("#content div:first").fadeIn(); // Show first tab content
    $('#tabs a').click(function (e) {
        e.preventDefault();
        $("#content > div").hide(); //Hide all content
        $("#tabs li").attr("id", ""); //Reset id's
        $(this).parent().attr("id", "current"); // Activate this
        $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab
    });
});